home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Miscellaneous / ival / name.c < prev    next >
Text File  |  1987-05-26  |  9KB  |  334 lines

  1. /*
  2.  * name.c - routines handling the "Name" window pane.
  3.  */
  4.  
  5. #include <quickdraw.h>
  6. #include <memory.h>
  7. #include <window.h>
  8. #include <toolutil.h>
  9. #include <packages.h>
  10. #include <control.h>
  11. #define ALLDIM 255 /* should be in control.h, but isn't */
  12. #include <font.h>
  13.  
  14. #include "ival.h"
  15. #include "progerr.h"
  16.  
  17. /*
  18.  * nabuts[] - buttons for the "Name" window pane
  19.  */
  20.  
  21. #define NASTRS        136    /* STR#    */
  22. #define NA_GIVE         0    /* "Give Name"        */
  23. #define NA_COUNT     1
  24. static ControlHandle nabuts[NA_COUNT];
  25.  
  26. /*
  27.  * updown[] - the button column specifying interval direction.
  28.  */
  29.  
  30. #define UDSTRS        131    /* STR# of labels for up/down names    */
  31. #define UD_ABOVE     0    /* index (not strid) of "above"        */
  32. #define UD_BELOW     1    /* below                */
  33. #define UD_COUNT     2    /* number of updown[] items        */
  34. static ControlHandle updown[UD_COUNT];
  35.  
  36. /*
  37.  * itype[] - the button column specifying interval type.
  38.  *   Indexed by IT_* (see ival.h).
  39.  */
  40.  
  41. #define ITSTRS        132    /* STR# of labels for interval types    */
  42. static ControlHandle itype[IT_COUNT];
  43.  
  44. /*
  45.  * isize[] - the button column specifying interval size.
  46.  * Indexed by isizectl[degree].
  47.  *
  48.  * isizectl[] - indexed by a degree, isizectl[] gives an index into isize[].
  49.  * Using isizectl, the interval size buttons don't have to be by order of degree.
  50.  */
  51.  
  52. #define ISSTRS        133    /* STR# of labels for interval sizes    */
  53. #define IS_OCT         0    /* octave    */
  54. #define IS_7th         1    /* seventh    */
  55. #define IS_6th         2    /* sixth    */
  56. #define IS_5th         3    /* fifth    */
  57. #define IS_4th         4    /* fourth    */
  58. #define IS_3rd         5    /* third    */
  59. #define IS_2nd         6    /* second    */
  60. #define IS_UNI         7    /* unison    */
  61. #define IS_COUNT     8    /* number of isize[] items        */
  62. static ControlHandle isize[IS_COUNT];
  63.  
  64. static int isizectl[IS_COUNT] = {
  65.     IS_UNI, IS_2nd, IS_3rd, IS_4th, IS_5th, IS_6th, IS_7th, IS_OCT
  66. };
  67.  
  68. /*
  69.  * fillname() - create the "name" window pane and its contents.
  70.  */
  71.  
  72. fillname()
  73. {
  74.     Rect windrect;    /* rect enclosing the window            */
  75.     short oldfont;    /* original font of this port            */
  76.     short numstrs;    /* number of strings in the current STR# resource*/
  77.     Rect udrect;    /* rect enclosing the updown[] buttons        */
  78.     Rect itrect;    /* rect enclosing the itype[] buttons        */
  79.     Rect isrect;    /* rect enclosing the isize[] buttons        */
  80.     Point delta;    /* a temporary distance                */
  81.     short width;    /* a width (in pixels)                */
  82.     Rect tmprect;    /* a temporary variable                */
  83.  
  84.     topLeft(windrect) = topLeft(windoc->portRect);
  85.     botRight(windrect) = botRight(windoc->portRect);
  86.     
  87.     oldfont = thePort->txFont;
  88.     TextFont(BUTTONFONT);
  89.     
  90.     /*
  91.      * get the rects that bound the columns of interval name buttons:
  92.      * The itype[] and updown[] buttons occupy one column.
  93.      */
  94.  
  95.     udrect.top = 0;
  96.     udrect.left = 0;
  97.     udrect.right = udrect.left + strswidth(UDSTRS, &numstrs) + BUTEXTRAH;
  98.     udrect.bottom = udrect.top + numstrs * (BUTHEIGHT + BUTVMARG);
  99.     
  100.     itrect.top = 0;
  101.     itrect.left = 0;
  102.     itrect.right = itrect.left + strswidth(ITSTRS, &numstrs) + BUTEXTRAH;
  103.     itrect.bottom = itrect.top + numstrs * (BUTHEIGHT + BUTVMARG);
  104.     
  105.     isrect.top = 0;
  106.     isrect.left = 0;
  107.     isrect.right = isrect.left + strswidth(ISSTRS, &numstrs) + BUTEXTRAH;
  108.     isrect.bottom = isrect.top + numstrs * (BUTHEIGHT + BUTVMARG);
  109.     TextFont(oldfont);
  110.     
  111.     if (udrect.right < itrect.right) {
  112.     udrect.right = itrect.right;
  113.     }
  114.     itrect.right = udrect.right;
  115.     OffsetRect(&isrect, udrect.right + HMARGIN, 0);
  116.     OffsetRect(&itrect, 0, isrect.bottom - itrect.bottom);
  117.     
  118.     /*
  119.      * Now the columns are in the correct relative places.
  120.      * Offset them to the appropriate place in the window,
  121.      */
  122.     
  123.     delta.h = windrect.left + HMARGIN;
  124.     delta.v = windrect.bottom - HMARGIN - BUTHEIGHT - HMARGIN;
  125.     delta.h -= itrect.left;
  126.     delta.v -= itrect.bottom;
  127.     
  128.     OffsetRect(&udrect, delta.h, delta.v);
  129.     OffsetRect(&itrect, delta.h, delta.v);
  130.     OffsetRect(&isrect, delta.h, delta.v);
  131.     
  132.     /*
  133.      * define the global Rect enclosing the Name pane.
  134.      */
  135.  
  136.     namerect.left = windrect.left + HMARGIN;
  137.     namerect.right = isrect.right;
  138.     namerect.top = isrect.top;
  139.     namerect.bottom = windrect.bottom - HMARGIN;
  140.     
  141.     /*
  142.      * Create the columns of Name controls.
  143.      */
  144.  
  145.     delta.h = 0;
  146.     delta.v = BUTHEIGHT + BUTVMARG;
  147.     topLeft(tmprect) = topLeft(udrect);
  148.     botRight(tmprect) = botRight(udrect);
  149.     tmprect.bottom = tmprect.top + BUTHEIGHT;
  150.     makeradcol(updown, RADPROCID, UDSTRS, &tmprect, delta.h, delta.v);
  151.     
  152.     topLeft(tmprect) = topLeft(itrect);
  153.     botRight(tmprect) = botRight(itrect);
  154.     tmprect.bottom = tmprect.top + BUTHEIGHT;
  155.     makeradcol(itype, RADPROCID, ITSTRS, &tmprect, delta.h, delta.v);
  156.     
  157.     topLeft(tmprect) = topLeft(isrect);
  158.     botRight(tmprect) = botRight(isrect);
  159.     tmprect.bottom = tmprect.top + BUTHEIGHT;
  160.     makeradcol(isize, RADPROCID, ISSTRS, &tmprect, delta.h, delta.v);
  161.  
  162.     /*
  163.      * Put the miscellaneous button(s) in their appropriate place:
  164.      * Name buttons are right justified just above the bottom.
  165.      */
  166.     
  167.     width = strswidth(NASTRS, &numstrs) + BUTEXTRAH;
  168.     tmprect.left = namerect.right - (width + BUTHMARG) * numstrs;
  169.     tmprect.right = tmprect.left + width;
  170.     tmprect.bottom = namerect.bottom;
  171.     tmprect.top = tmprect.bottom - BUTHEIGHT;
  172.     delta.h = width + BUTHMARG;
  173.     delta.v = 0;
  174.     makeradcol(nabuts, BUTPROCID, NASTRS, &tmprect, delta.h, delta.v);
  175.  
  176.     TextFont(oldfont);
  177. }
  178.  
  179. /*
  180.  * newname() - reset the name controls, based on the new test state.
  181.  */
  182.  
  183. newname()
  184. {
  185.     short i;
  186.     int is_above;
  187.  
  188.     /*
  189.      * always give the above/below buttons
  190.      */
  191.  
  192.     if (curtone[1].degree > curtone[0].degree) {
  193.         is_above = TRUE;
  194.     } else if (curtone[1].degree < curtone[0].degree) {
  195.         is_above = FALSE;
  196.     } else if (curtone[1].chrome >= curtone[0].chrome) {
  197.         is_above = TRUE;
  198.     } else {
  199.         is_above = FALSE;
  200.     }
  201.  
  202.     if (is_above) {
  203.     SetCtlValue(updown[UD_ABOVE], 1);
  204.     SetCtlValue(updown[UD_BELOW], 0);
  205.     } else {
  206.     SetCtlValue(updown[UD_ABOVE], 0);
  207.     SetCtlValue(updown[UD_BELOW], 1);
  208.     }
  209.     
  210.     /*
  211.      * clear out the type and size buttons
  212.      */
  213.  
  214.     for (i = 0; i < IT_COUNT; ++i) {
  215.     HiliteControl(itype[i], 0);
  216.     SetCtlValue(itype[i], 0);
  217.     }
  218.     for (i = 0; i < IS_COUNT; ++i) {
  219.     HiliteControl(isize[i], 0);
  220.     SetCtlValue(isize[i], 0);
  221.     }
  222.     
  223.     /*
  224.      * Set the appropriate buttons (based on who does what).
  225.      */
  226.  
  227.     if (!progtasks.name) {
  228.     HiliteControl(nabuts[NA_GIVE], 0);
  229.     isizeshown = FALSE;
  230.     itypeshown = FALSE;
  231.     } else {
  232.     HiliteControl(nabuts[NA_GIVE], ALLDIM);
  233.     SetCtlValue(itype[curitype], 1);
  234.     itypeshown = TRUE;
  235.     SetCtlValue(isize[isizectl[curisize]], 1);
  236.     isizeshown = TRUE;
  237.     }
  238. }
  239.  
  240. /*
  241.  * namepress() - handle a button-press in the Name pane.
  242.  */
  243.  
  244. namepress(whichcontrol)
  245. ControlHandle whichcontrol;
  246. {
  247.     short oldvalue;
  248.     short i;
  249.     
  250.     /*
  251.      * find which button (and class of button) was pressed.
  252.      */
  253.     
  254.     for (i = 0; i < NA_COUNT; ++i) {
  255.     if (whichcontrol == nabuts[i]) {
  256.  
  257.         /*
  258.          * "Give" button.  Give the user one part of the interval.
  259.          */
  260.  
  261.         if (!isizeshown) {
  262.         SetCtlValue(isize[isizectl[curisize]], 1);
  263.         isizeshown = TRUE;
  264.         } else if (!itypeshown) {
  265.         SetCtlValue(itype[curitype], 1);
  266.         itypeshown = TRUE;
  267.  
  268.         HiliteControl(nabuts[NA_GIVE], ALLDIM);
  269.         }
  270.         break;
  271.     }
  272.     }
  273.     for (i = 0; i < UD_COUNT; ++i) {
  274.     if (whichcontrol == updown[i]) {
  275.         /* ignore it */
  276.         break;
  277.     }
  278.     }
  279.     for (i = 0; i < IT_COUNT; ++i) {
  280.     if (whichcontrol == itype[i]) {
  281.     
  282.         /*
  283.          * An itype press.
  284.          * If they got it right, light that button;
  285.          * otherwise, dim that button.
  286.          */
  287.  
  288.         if (!itypeshown) {
  289.         if (i == curitype) {
  290.             SetCtlValue(itype[curitype], 1);
  291.             itypeshown = TRUE;
  292.  
  293.             if (isizeshown) {
  294.             HiliteControl(nabuts[NA_GIVE], ALLDIM);
  295.             }
  296.         } else {
  297.             if (!isizeshown) {
  298.             /*XXX deal with enharmonic equivalents */
  299.             }
  300.             HiliteControl(itype[i], ALLDIM);
  301.         }
  302.         }
  303.         break;
  304.     }
  305.     }
  306.     for (i = 0; i < IS_COUNT; ++i) {
  307.     if (whichcontrol == isize[i]) {
  308.     
  309.         /*
  310.          * An isize press.
  311.          * If they got it right, light that button;
  312.          * otherwise, dim that button.
  313.          */
  314.  
  315.         if (!isizeshown) {
  316.         if (i == isizectl[curisize]) {
  317.             SetCtlValue(isize[isizectl[curisize]], 1);
  318.             isizeshown = TRUE;
  319.  
  320.             if (itypeshown) {
  321.             HiliteControl(nabuts[NA_GIVE], ALLDIM);
  322.             }
  323.         } else {
  324.             if (!itypeshown) {
  325.             /*XXX deal with enharmonic equivalents */
  326.             }
  327.             HiliteControl(isize[i], ALLDIM);
  328.         }
  329.         }
  330.         break;
  331.     }
  332.     }
  333. }
  334.